package com.bd.rj.backstage.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bd.rj.backstage.service.IEnterpriseService;
import com.bd.rj.backstage.service.IUserService;
import com.bd.rj.backstage.service.UploadImageService;
import com.bd.rj.common.entity.Enterprise;
import com.bd.rj.common.entity.User;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/enterprise")
public class EnterpriseController {
    @Resource
    IEnterpriseService enterpriseService;
    @Resource
    UploadImageService uploadImageService;
    @Resource
    IUserService userService;

    /**
     * 分页+多条件查询,当使用该方法的用户为老师时,仅查看该老师所负责的企业
     *
     * @author 白世俊
     * @date 2025/1/5
     */
    //todo 现在接口不同,把鉴权关了www
//    @SaCheckRole(value = {"1","2"},mode = SaMode.OR)
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public Map<String, Object> queryCondition(
            @RequestParam(value = "pagenum", defaultValue = "1") int pagenum,
            @RequestParam(value = "pagesize", defaultValue = "10") int pageSize,
            @RequestParam(required = false) String ename,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) Integer status) {
        Enterprise enterprise = new Enterprise();

//        获取当前角色信息,若使用此方法的为教师角色,将只返回此角色所属的企业.
        String role = StpUtil.getRoleList().get(0);
        if (role.equals("2")) {
            enterprise.setUid(StpUtil.getLoginIdAsLong());
        }

        enterprise.setEname(ename);
        enterprise.setType(type);
        enterprise.setStatus(status);
        //根据条件进行查询
        IPage<Map<String, Object>> enterpriseByPage = enterpriseService.getEnterpriseByConditionAndPage(pagenum, pageSize, enterprise);
        enterpriseByPage.getRecords().stream();
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", enterpriseByPage);
        return map;
    }

    /**
     * @desc 获取单个企业信息
     * @author 白世俊
     * @date 2025/1/6
     */
    //todo 鉴权信息关闭
//    @SaCheckRole(value = "1")
    @RequestMapping(value = "/queryone")
    public Map<String, Object> getInfo(@RequestParam(value = "id") Long id) {
        Enterprise enterprise = enterpriseService.getById(id);
        Long uid = enterprise.getUid();
        User user = userService.getById(uid);
        String realname = user.getRealname();
        Map<String, Object> enterpriseInfo = new HashMap<>();
        enterpriseInfo.put("enterprise", enterprise);
        enterpriseInfo.put("realname", realname);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "获取企业信息成功");
        map.put("data", enterpriseInfo);
        return map;
    }

    @RequestMapping(value = "getCompanyName", method = RequestMethod.GET)
    public Map<String,Object> getCompanyName() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("uid", StpUtil.getLoginIdAsLong());
        List list = enterpriseService.list(queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "获取成功");
        map.put("data", list);
        return map;
    }

    /**
     * 批量删除
     *
     * @author 白世俊
     * @date 2025/1/5
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Map<String, Object> delete(@RequestParam(value = "id") List<Long> ids) {
        boolean result = enterpriseService.removeBatchByIds(ids);
        IPage<Map<String, Object>> enterpriseByConditionAndPage = null;
        Enterprise condition = new Enterprise();
        if (StpUtil.getRoleList().get(0).equals("2")) {
            condition.setUid(StpUtil.getLoginIdAsLong());
            enterpriseByConditionAndPage = enterpriseService.getEnterpriseByConditionAndPage(1, 10, condition);
        }else {
            enterpriseByConditionAndPage = enterpriseService.getEnterpriseByConditionAndPage(1, 10, null);
        }

        Map<String, Object> map = new HashMap<>();
        if (result) {
            map.put("code", 200);
            map.put("msg", "删除成功");
            map.put("data", enterpriseByConditionAndPage);
        } else {
            map.put("code", 201);
            map.put("msg", "删除失败");
            map.put("data", enterpriseByConditionAndPage);
        }
        return map;
    }

    /**
     * @desc 新增企业
     * @author 白世俊
     * @date 2025/1/5
     */
    @SaCheckRole(mode = SaMode.OR, value = {"1", "2"})
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Map<String, Object> add(@RequestBody Enterprise enterprise) {
        //添加老师信息
        long userId = StpUtil.getLoginIdAsLong();
        enterprise.setUid(userId);
        boolean result = enterpriseService.save(enterprise);
        Map<String, Object> map = new HashMap<>();
        if (result) {
            map.put("code", 200);
            map.put("msg", "添加成功");
            map.put("data", enterprise);
            return map;
        } else {
            map.put("code", 201);
            map.put("msg", "添加失败,可能是公司名称已经注册在本平台");
            map.put("data", enterprise);
            return map;
        }
    }

    /**
     * @desc 上传企业资质图片 *1张
     * @author 白世俊
     * @date 2025/1/6
     */
    @RequestMapping(value = "/uploadenteimg", method = RequestMethod.POST)
    public Map<String, Object> uploadEnterpriseImg(@RequestParam("file") MultipartFile file) {
        Map<String, Object> map = new HashMap<>();
        if (!file.isEmpty()) {
            //参数1:上传的文件
            //参数2:文件要上传至的服务器文件夹
            String path = uploadImageService.uploadQNFile(file, "enterprise_img/");
            if (path.equals("失败")) {
                map.put("code", 201);
                map.put("msg", "上传失败");
                map.put("data", file);
            } else {
                System.out.println("七牛云图片上传成功" + path);
                map.put("code", 200);
                map.put("msg", "上传成功");
                map.put("data", path);
            }
        }
        return map;
    }

    /**
     * @desc 审核接口, 0待审核, 1通过
     * @author 白世俊
     * @date 2025/1/5
     */
    @SaCheckRole(value = "1")
    @RequestMapping(value = "/dostatus", method = RequestMethod.PUT)
    public Map<String, Object> status(@RequestParam(value = "id") Long id, @RequestParam(value = "status") Integer status) {
        //进行审核操作
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", id);
        Enterprise enterprise = new Enterprise();
        enterprise.setStatus(status);
        enterpriseService.update(enterprise, queryWrapper);
        //插入审核记录
        Map<String, Object> recordInfoMap = new HashMap<>();
        recordInfoMap.put("uid", StpUtil.getLoginId());
        recordInfoMap.put("eid", id);
        recordInfoMap.put("status", status);
        enterpriseService.insertEnterpriseRecord(recordInfoMap);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "审核成功");
        map.put("data", id);
        return map;
    }
//
//    /**
//     * @desc 返回企业类型列表
//     * @author 白世俊
//     * @date 2025/1/13
//     */
//    @RequestMapping(value = "/getEnterpriseType", method = RequestMethod.GET)
//    public Map<String, Object> getEnterpriseType() {
//        List<Map<String, String>> companyTypes = new ArrayList<>();
//
//        // 将每个企业类型作为值，键为 "name"
//        companyTypes.add(createMap("IT"));
//        companyTypes.add(createMap("制造业"));
//        companyTypes.add(createMap("金融"));
//        companyTypes.add(createMap("医疗"));
//        companyTypes.add(createMap("零售"));
//        companyTypes.add(createMap("房地产"));
//        companyTypes.add(createMap("教育"));
//        companyTypes.add(createMap("能源"));
//        companyTypes.add(createMap("物流"));
//        companyTypes.add(createMap("娱乐"));
//        companyTypes.add(createMap("咨询"));
//
//        Map<String, Object> map = new HashMap<>();
//        map.put("code", 200);
//        map.put("msg", "获取成功");
//        map.put("data", companyTypes);
//        return map;
//    }

    private static Map<String, String> createMap(String value) {
        Map<String, String> map = new HashMap<>();
        map.put("name", value);
        return map;
    }

    @RequestMapping(value = "getIndustries", method = RequestMethod.GET)
    public Map<String,Object> getIndustries() {
        List<Map<String, Object>> industries = enterpriseService.getIndustries();
        List<Map<String, Object>> result = enterpriseService.buildHierarchy(industries);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("data", result);
        return map;
    }


}
